package com.example.likegifmarker.controller.activities;

import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;

import com.example.likegifmarker.Model.Beans.ChangeEvent;
import com.example.likegifmarker.R;
import com.example.likegifmarker.controller.fragments.BaseFragment;
import com.example.likegifmarker.controller.fragments.CityFragment;
import com.example.likegifmarker.controller.fragments.IndexFragment;
import com.example.likegifmarker.controller.fragments.MsgFragment;
import com.example.likegifmarker.controller.fragments.MyFragment;
import com.example.likegifmarker.view.wdigets.MainTabBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements MainTabBar.OnTabSelectedListener {


    @BindView(R.id.mtb)
    MainTabBar mMtb;

    public int getSelectedIndex() {
        return selectedIndex;
    }

    private int selectedIndex = -1;
    private BaseFragment[] mContentFragments;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        permissionHelper.onRequestPermissionsResult(this, requestCode);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {
        setFullScreen();
        mContentFragments = new BaseFragment[]{
                new IndexFragment(), new CityFragment(), new IndexFragment(), new MsgFragment(), new MyFragment()
        };
        mMtb.setOnTabSelectedListener(this);
        onSelected(0);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(0x00000000);
    }


    private BaseFragment getFragment(int idx) {
        if (idx == -1) return null;
        return mContentFragments[idx];
    }

    @Override
    public boolean onSelected(int idx) {
        if (selectedIndex != idx) {
            EventBus.getDefault().post(new ChangeEvent(idx));
        }
        setAndroidNativeLightStatusBar(this,idx != 0);
        if (idx == 0) {
            mMtb.setBg(false, idx);
        } else {
            mMtb.setBg(true, idx);
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        BaseFragment fragment = getFragment(idx);
        BaseFragment currentFragment = getFragment(selectedIndex);
        selectedIndex = idx;

        if (currentFragment == null) {
            transaction.add(R.id.fl_container, fragment).commitAllowingStateLoss();
            return true;
        }

        if (!fragment.isAdded() && getSupportFragmentManager().findFragmentByTag("" + selectedIndex) == null) {
            transaction.hide(currentFragment).add(R.id.fl_container, fragment, "" + selectedIndex).commitAllowingStateLoss();
        } else {
            transaction.hide(currentFragment).show(fragment).commitAllowingStateLoss();
        }
        return true;
    }
}